package com.atguigu.lease.web.admin.custom.config.interceptor;

import com.atguigu.lease.common.Exceptions.LeaseException;
import com.atguigu.lease.common.jwtUtils.JwtUtil;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @Author zy
 * @Date 2024/12/13
 * @Desc
 * @Version 1.0
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求头
        String token = request.getHeader("access_token");
        //判断是否为NUll
        if(ObjectUtils.isEmpty(token)){
            throw new LeaseException(ResultCodeEnum.ADMIN_LOGIN_AUTH);
        }
        //判断jwt--parseToken()
        JwtUtil.parseToken(token);
        //放行
        return true;
    }
}
